package com.wy.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.common.Result;
import com.wy.dto.GoodsAuctionDto;
import com.wy.entity.GoodsAuction;
import com.wy.entity.GoodsInfo;
import com.wy.mapper.GoodsAuctionMapper;
import com.wy.mapper.GoodsInfoMapper;
import com.wy.service.GoodsAuctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author:王渝
 * @Date: 2022/11/30  11:07
 * @Version 1.0
 */
@Service
public class GoodsAuctionServiceImpl extends ServiceImpl<GoodsAuctionMapper, GoodsAuction> implements GoodsAuctionService {

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsAuctionMapper goodsAuctionMapper;


    @Transactional(rollbackFor = {})
    @Override
    public boolean participateAuction(GoodsAuction goodsAuction, boolean isParticipate) {
        boolean save;
        //是否参与拍了
        if(isParticipate){
            //参与了
            LambdaUpdateWrapper<GoodsAuction> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(GoodsAuction::getAuctionId,goodsAuction.getAuctionId());
            lambdaUpdateWrapper.set(GoodsAuction::getBidAmount,goodsAuction.getBidAmount());
            //update
            save = this.update(lambdaUpdateWrapper);
        }else {
            //insert
            save = this.save(goodsAuction);
        }
        //是否更新成功
        if(save){
            //将最高价用户 修改为本用户
            int i = goodsInfoMapper.updateCurrentUserId(goodsAuction.getUserId(), goodsAuction.getGoodsId());
            if(i < 1){
                //修改失败 回滚数据
                throw new RuntimeException("出价失败");
            }
            //查询当前价格
            BigDecimal bigDecimal = goodsInfoMapper.queryMaxPriceByGoodsId(goodsAuction.getGoodsId());
            if(bigDecimal.compareTo(goodsAuction.getBidAmount()) < 0){
                //再次查询商品价格（防止出价时又有人出价）  当前价格大于自己的价格
                //抛出异常回滚数据
                throw new RuntimeException("出价失败  出价小于当前价格");
            }
            return true;

        }

        return false;
    }

    @Override
    public PageInfo<GoodsAuctionDto> queryPageByUserId(int currentPage, int pageSize, Long userId, Boolean isEnd) {
        List<GoodsAuctionDto> list = null;
        PageHelper.startPage(currentPage,pageSize);
        if(isEnd){
            //已经结果的
            list = goodsAuctionMapper.queryInfoByUserIdIsEnd(userId);
        }else {
            //正在进行中的
            list = goodsAuctionMapper.queryInfoByUserIdIng(userId);
        }
        PageInfo<GoodsAuctionDto> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<GoodsAuction> queryListByGoodsId(Long goodsId) {
        return goodsAuctionMapper.queryListByGoodsId(goodsId);
    }


}
